import axios from 'axios';
import moment from 'moment';
import { vendorsFmtDate } from './../../utils/config';

moment.locale('zh-cn');

const ConstValues = {
    Options: [
        { key: "7", value: "审批中" },
        { key: "9", value: "审批通过" },
        { key: "8", value: "已驳回" },
    ],
    Primarykey: 'pkWorkstopreport'
};

// 停工单保存
const saveStopWorkBill = (param) => {
    return axios({
        method: 'post',
        url: '/workStop/add',
        data: param,
        headers: {
            'Cache-Control': 'no-cache',
        }
    })
}

// 停工单获取单条数据
function getStopworkById({ record, callback }) {
    let primaryKeyValue = record[ConstValues.Primarykey];
    this.props.ajax({
        method: 'get',
        url: '/workStop/getReportById',
        data: { reportId: primaryKeyValue },
        success: (res) => {
            callback(res);
        },
        error: (msg) => {

        }
    });
}

// 获取租户信息
const getTenantId = () => {
    let tenantId = document.cookie.split(';').find(cur => cur.indexOf('tenantid=') != -1).split('=')[1]; // 租户ID
    // let tenantId = "x61tnmfx";
    return tenantId;
}

// 整理要保存的数据
const formatPostData = (data) => {
    let {
        scode,
        sname,
        position,
        stopDate,
        preresumeDate,
        workstopdays,
        userId,
        pkProject,
        workstopreason,
    } = data;
    position = position ? position : '';
    workstopdays = workstopdays ? workstopdays : '';
    stopDate = stopDate ? vendorsFmtDate(stopDate) : vendorsFmtDate(new Date().getDate());
    preresumeDate = preresumeDate ? vendorsFmtDate(preresumeDate) : vendorsFmtDate(new Date().getDate());
    let copyPersonsArr = userId ? userId.map((item) => {
        return { ['copyPerson']: item };
    }) : [];
    let copyPersons = userId ? copyPersonsArr : [];
    let projectName = sname ? sname : '';
    let projectCode = scode ? scode : '';
    let params = {
        copyPersons,
        pkTenant: getTenantId(),
        pkProject,
        projectName,
        projectCode,
        position,
        billstatus: 5, // 固定自由态
        billType: 'TGBG',
        stopDate,
        preresumeDate,
        workstopdays,
        workstopreason: workstopreason ? workstopreason : '',
    }
    return params;
}

// 渲染页面
function initPage({ result, pageStatus }) {
    if (result) {
        let {
            copyPersons,
            pkProject,
            orgName,
            projectName,
            projectCode,
            position,
            billstatus,
            stopDate,
            preresumeDate,
            workstopdays,
            workstopreason,
        } = result;
        let strBillstatus = ConstValues.Options.filter((item) => item.key == billstatus); // 前端翻译单据状态
        let copyPersonNames = copyPersons ? copyPersons.map((item) => item.copyPersonName) : []; // 抄送人信息
        this.props.form.setFormItemsValue({
            'workstopdays': { value: workstopdays },
            'pkProject': { value: pkProject },
            'orgName': { value: orgName },
            'sname': { value: projectName },
            'scode': { value: projectCode, display: projectCode },
            'position': { value: position },
            'strBillstatus': { value: strBillstatus[0].value },
            'stopDate': { value: moment(stopDate) },
            'preresumeDate': { value: moment(preresumeDate) },
            'workstopreason': { value: workstopreason },
            'userId': { value: copyPersonNames.join(','), display: copyPersonNames.join(',') }, // 抄送人
        });
    } else {
        this.props.form.setFormItemsValue({
            'workstopdays': { value: '' },
            'pkProject': { value: '' },
            'orgName': { value: '' },
            'sname': { value: '' },
            'scode': { value: '', display: '' },
            'position': { value: '' },
            'strBillstatus': { value: '自由态' },
            'stopDate': { value: moment(new Date()) },
            'preresumeDate': { value: '' },
            'workstopreason': { value: '' },
            'userId': { value: '', display: '' }, // 抄送人
        });
    }
    this.props.button.setButtonState(pageStatus);
    this.props.form.setFormStatus(pageStatus);
    this.props.approve.closeApproveModal();
    this.setState({ pageStatus, result });
}

export {
    initPage,
    ConstValues,
    getTenantId,
    getStopworkById,
    formatPostData,
    saveStopWorkBill
}